<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: mcastClient.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>mcastClient.c</h1><a href="mcast_client_8c.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* fpont 12/99   */</span>
<a name="l00002"></a>00002 <span class="comment">/* pont.net      */</span>
<a name="l00003"></a>00003 <span class="comment">/* mcastClient.c */</span>
<a name="l00004"></a>00004 
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;sys/socket.h&gt;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;netinet/in.h&gt;</span>
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;arpa/inet.h&gt;</span>
<a name="l00009"></a>00009 <span class="preprocessor">#include &lt;netdb.h&gt;</span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;unistd.h&gt;</span> <span class="comment">/* close */</span>
<a name="l00012"></a>00012 
<a name="l00013"></a><a class="code" href="mcast_client_8c.html#c42367fe5c999ec6650de83e9d72fe8c">00013</a> <span class="preprocessor">#define SERVER_PORT 1500</span>
<a name="l00014"></a><a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">00014</a> <span class="preprocessor"></span><span class="preprocessor">#define MAX_MSG 100</span>
<a name="l00015"></a>00015 <span class="preprocessor"></span>
<a name="l00016"></a><a class="code" href="mcast_client_8c.html#0ddf1224851353fc92bfbff6f499fa97">00016</a> <span class="keywordtype">int</span> <a class="code" href="test__hashtable_8c.html#3c04138a5bfe5d72780bb7e82a18e627">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) {
<a name="l00017"></a>00017 
<a name="l00018"></a>00018   <span class="keywordtype">int</span> sd, rc, i;
<a name="l00019"></a>00019   <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> ttl = 1;
<a name="l00020"></a>00020   <span class="keyword">struct </span>sockaddr_in cliAddr, servAddr;
<a name="l00021"></a>00021   <span class="keyword">struct </span>hostent *h;
<a name="l00022"></a>00022 
<a name="l00023"></a>00023   <span class="keywordflow">if</span>(argc&lt;3) {
<a name="l00024"></a>00024     printf(<span class="stringliteral">"usage %s &lt;mgroup&gt; &lt;data1&gt; &lt;data2&gt; ... &lt;dataN&gt;\n"</span>,argv[0]);
<a name="l00025"></a>00025     exit(1);
<a name="l00026"></a>00026   }
<a name="l00027"></a>00027 
<a name="l00028"></a>00028   h = gethostbyname(argv[1]);
<a name="l00029"></a>00029   <span class="keywordflow">if</span>(h==NULL) {
<a name="l00030"></a>00030     printf(<span class="stringliteral">"%s : unknown host '%s'\n"</span>,argv[0],argv[2]);
<a name="l00031"></a>00031     exit(1);
<a name="l00032"></a>00032   }
<a name="l00033"></a>00033 
<a name="l00034"></a>00034   servAddr.sin_family = h-&gt;h_addrtype;
<a name="l00035"></a>00035   memcpy((<span class="keywordtype">char</span> *) &amp;servAddr.sin_addr.s_addr, h-&gt;h_addr_list[0],h-&gt;h_length);
<a name="l00036"></a>00036   servAddr.sin_port = htons(<a class="code" href="mcast_client_8c.html#c42367fe5c999ec6650de83e9d72fe8c">SERVER_PORT</a>);
<a name="l00037"></a>00037   
<a name="l00038"></a>00038   <span class="comment">/* check if dest address is multicast */</span>
<a name="l00039"></a>00039   <span class="keywordflow">if</span>(!IN_MULTICAST(ntohl(servAddr.sin_addr.s_addr))) {
<a name="l00040"></a>00040     printf(<span class="stringliteral">"%s : given address '%s' is not multicast \n"</span>,argv[0],
<a name="l00041"></a>00041            inet_ntoa(servAddr.sin_addr));
<a name="l00042"></a>00042     exit(1);
<a name="l00043"></a>00043   }
<a name="l00044"></a>00044 
<a name="l00045"></a>00045   <span class="comment">/* create socket */</span>
<a name="l00046"></a>00046   sd = socket(AF_INET,SOCK_DGRAM,0);
<a name="l00047"></a>00047   <span class="keywordflow">if</span> (sd&lt;0) {
<a name="l00048"></a>00048     printf(<span class="stringliteral">"%s : cannot open socket\n"</span>,argv[0]);
<a name="l00049"></a>00049     exit(1);
<a name="l00050"></a>00050   }
<a name="l00051"></a>00051   
<a name="l00052"></a>00052   <span class="comment">/* bind any port number */</span>
<a name="l00053"></a>00053   cliAddr.sin_family = AF_INET;
<a name="l00054"></a>00054   cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
<a name="l00055"></a>00055   cliAddr.sin_port = htons(0);
<a name="l00056"></a>00056   <span class="keywordflow">if</span>(bind(sd,(<span class="keyword">struct</span> sockaddr *) &amp;cliAddr,<span class="keyword">sizeof</span>(cliAddr))&lt;0) {
<a name="l00057"></a>00057     perror(<span class="stringliteral">"bind"</span>);
<a name="l00058"></a>00058     exit(1);
<a name="l00059"></a>00059   }
<a name="l00060"></a>00060 
<a name="l00061"></a>00061   <span class="keywordflow">if</span>(setsockopt(sd,IPPROTO_IP,IP_MULTICAST_TTL, &amp;ttl,<span class="keyword">sizeof</span>(ttl))&lt;0) {
<a name="l00062"></a>00062     printf(<span class="stringliteral">"%s : cannot set ttl = %d \n"</span>,argv[0],ttl);
<a name="l00063"></a>00063     exit(1);
<a name="l00064"></a>00064   }
<a name="l00065"></a>00065 
<a name="l00066"></a>00066   printf(<span class="stringliteral">"%s : sending data on multicast group '%s' (%s)\n"</span>,argv[0],
<a name="l00067"></a>00067          h-&gt;h_name,inet_ntoa(*(<span class="keyword">struct</span> in_addr *) h-&gt;h_addr_list[0]));
<a name="l00068"></a>00068   
<a name="l00069"></a>00069 
<a name="l00070"></a>00070   <span class="comment">/* send data */</span>
<a name="l00071"></a>00071   <span class="keywordflow">for</span>(i=2;i&lt;argc;i++) {
<a name="l00072"></a>00072     rc = sendto(sd,argv[i],strlen(argv[i])+1,0,
<a name="l00073"></a>00073                 (<span class="keyword">struct</span> sockaddr *) &amp;servAddr, <span class="keyword">sizeof</span>(servAddr));
<a name="l00074"></a>00074     
<a name="l00075"></a>00075     <span class="keywordflow">if</span> (rc&lt;0) {
<a name="l00076"></a>00076       printf(<span class="stringliteral">"%s : cannot send data %d\n"</span>,argv[0],i-1);
<a name="l00077"></a>00077       close(sd);
<a name="l00078"></a>00078       exit(1);
<a name="l00079"></a>00079     }
<a name="l00080"></a>00080 
<a name="l00081"></a>00081   }<span class="comment">/* end for */</span>
<a name="l00082"></a>00082 
<a name="l00083"></a>00083   <span class="comment">/* close socket and exit */</span>
<a name="l00084"></a>00084   close(sd);
<a name="l00085"></a>00085   exit(0);
<a name="l00086"></a>00086 }
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 22:06:25 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
